package com.designPattern.proxy.jdk;

/**
 * @Auther: zwg
 * @Date: 2019/2/20 20:02
 * @Description:
 */
public interface Calculator {
    //需要代理的接口
    public int add(int a,int b);

    public int subtraction(int a, int b);
    //接口实现类,执行真正的a+b操作
    public static class CalculatorImpl implements Calculator{
        @Override
        public int add(int a, int b) {
            int result = a + b;
            System.out.println("doing a+b="+result);
            return result;
        }

        @Override
        public int subtraction(int a, int b) {
            int result = a - b;
            System.out.println("doing a-b="+result);
            return result;
        }
    }
    //静态代理类的实现.代码已经实现好了.
    public static class CalculatorProxy implements Calculator{
        private Calculator calculator;
        public CalculatorProxy(Calculator calculator) {
            this.calculator=calculator;
        }
        @Override
        public int add(int a, int b) {
            //执行一些操作
            System.out.println("begin ");
            int result = calculator.add(a, b);
            System.out.println("end ");
            return result;
        }

        @Override
        public int subtraction(int a, int b) {
            //执行一些操作
            System.out.println("begin ");
            int result = calculator.subtraction(a, b);
            System.out.println("end ");
            return result;
        }
    }
}
